package com.whf.android.jar.view.item;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;


import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ScreenUtils;
import com.whf.android.jar.app.Latte;

import java.util.List;

/**
 * 水印
 *
 * @author qf
 * @version 1.0.2
 */
public class WatermarkDrawable extends Drawable {

    private final Paint paint = new Paint();
    private final List<String> labels;
    private final Context context;
    private final int angle;//水印角度
    private final int fontSize;//字体大小 单位sp


    /**
     * 初始化构造
     */
    public WatermarkDrawable() {
        this.context = Latte.getActivity();//上下文
        this.labels = Latte.watermark();//水印文字列表,多行显示支持
        this.angle = -30;//水印角度
        this.fontSize = 14;//水印文字大小
    }

    /**
     * 初始化构造
     */
    public WatermarkDrawable(Context context, List<String> labels) {
        this.context = context;//上下文
        this.labels = labels;//水印文字列表,多行显示支持
        this.angle = -30;//水印角度
        this.fontSize = 14;//水印文字大小
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int width = getBounds().right;
        int height = getBounds().bottom;

        canvas.drawColor(Color.parseColor("#40F3F5F9"));
        paint.setColor(Color.parseColor("#50AEAEAE"));

        paint.setAntiAlias(true);
        paint.setTextSize(sp2px(context, fontSize));
        canvas.save();
        canvas.rotate(angle);
        float textWidth = paint.measureText(labels.get(0));
        int index = 0;
        for (int positionY = height / 10; positionY <= height; positionY += height / 10 + 80) {
            float fromX = -width + (index++ % 2) * textWidth;
            for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
                int spacing = 0;//间距
                for (String label : labels) {
                    canvas.drawText(label, positionX, positionY + spacing, paint);
                    spacing = spacing + 50;
                }

            }
        }
        canvas.restore();
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }


    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public Bitmap getBitmap() {
        // 取 drawable 的长宽
        int w = ScreenUtils.getAppScreenWidth();
        int h = ScreenUtils.getAppScreenHeight();
        // 取 drawable 的颜色格式
        Bitmap.Config config = getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        draw(canvas);
        return bitmap;
    }
}